package tzhomestudio.framework.shared.utils;

import java.io.File;
import java.io.IOException;

/**
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class FileUtils {
    // Main data
    private static String workingDir = System.getProperty("user.dir");
    private static final String userHomeDir = System.getProperty("user.home");
    private static final String tmpHomeDir = getWorkingDir() + "tmp";

    public static String getWorkingDir() {
        return workingDir + File.separator;
    }

    public static void setWorkingDir(String workingDir) {
        FileUtils.workingDir = workingDir;
    }

    public static String getUserHomeDir() {
        return userHomeDir + File.separator;
    }

    public static String getTmpHomeDir() {
        return tmpHomeDir + File.separator;
    }

    public static void cleanDirectory(String directoryName) throws IOException {
        cleanDirectory(new File(directoryName));
    }

    public static void cleanDirectory(File directory) throws IOException {
        if (directory.isDirectory() && directory.list().length != 0) {
            //list all the directory contents
            String files[] = directory.list();

            for (String temp : files) {
                //construct the file structure
                File fileDelete = new File(directory, temp);

                //recursive delete
                delete(fileDelete);
            }
        }
    }

    public static void moveFilesFromDirectoryToAnotherDirectory(String srcDirName, String trgDirName) throws IOException {
        moveFilesFromDirectoryToAnotherDirectory(new File(srcDirName), new File(trgDirName));
    }

    public static void moveFilesFromDirectoryToAnotherDirectory(File srcDirName, File trgDirName) throws IOException {
        if(srcDirName.isDirectory()) {
            File[] content = srcDirName.listFiles();
            for (int i = 0; i < content.length; i++) {
                content[i].renameTo(new File(trgDirName + File.separator + content[i].getName()));
            }
        }
    }

    public static void delete(File file) throws IOException {
        if (file.isDirectory()) {
            //directory is empty, then delete it
            if(file.list().length==0){
                file.delete();
            } else {
                //list all the directory contents
                String files[] = file.list();

                for (String temp : files) {
                    //construct the file structure
                    File fileDelete = new File(file, temp);

                    //recursive delete
                    delete(fileDelete);
                }

                //check the directory again, if empty then delete it
                if(file.list().length==0){
                    file.delete();
                }
            }
        }else{
            //if file, then delete it
            file.delete();
        }
    }

    public static File createTmpFileWithName(String fileName) {
        String dirName = RandomUtils.generateEnglishRandomWord(16);
        File dirTmpFile = new File(getTmpHomeDir() + dirName);
        dirTmpFile.mkdirs();
        return new File (dirTmpFile.getAbsolutePath() + File.separator + fileName);
    }
}
